/*******************************************************************************
 * destal open world, an open source java multiplayer game
 * Copyright (C) 2011 Alexander Belke, Dennis Sternberg, Steffen Schneider
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package destal.client.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MenuPanel extends JPanel implements ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4586123693763691402L;
	private JButton[] _button;
	private JTextField _txtHostname;
	private JTextField _txtUsername;
	private GUI _gui;
	
	private final File saveFile = new File("data/config.ini");
	
	public MenuPanel (GUI gui)
	{
		super();
		initComponents();
		_gui = gui;
		
		if (!saveFile.exists())
		{
			try
			{
				saveFile.createNewFile();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		else
		{
			loadConnectionData();
		}		
	}
	
	private void initComponents()
	{
		_txtHostname = new JTextField("localhost");
		_txtHostname.setLocation(50, 50);
		_txtHostname.setSize(150, 20);
		this.add(_txtHostname);
		
		_txtUsername = new JTextField("username");
		_txtUsername.setLocation(350, 50);
		_txtUsername.setSize(150, 20);
		this.add(_txtUsername);

		_button = new JButton[]{new JButton ("Start Game"),
								new JButton ("Options"),
								new JButton ("Exit") };
		for (int i = 0; i < _button.length; i++)
		{
			_button[i].setLocation((i+1) * 100, 100);
			_button[i].setSize(100, 20);
			this.add(_button[i]);
			_button[i].addActionListener(this);
		}
	}
	
    @Override
    public void actionPerformed(ActionEvent e)
    {
    	if (e.getActionCommand().equals("Start Game"))
    	{
    		saveConnectionData();
    		_gui.getClient().connect(_txtHostname.getText(), _txtUsername.getText());
    	}
    	if (e.getActionCommand().equals("Options"))
    	{
    		_gui.setGUIMode (GUI.GUIMode.OPTIONS);
    	}
    	if (e.getActionCommand().equals("Exit"))
    	{
    		System.exit(0);
    	}
    }
    
    private void saveConnectionData()
    {
    	try
    	{
			FileOutputStream fout = new FileOutputStream(saveFile);
			Properties p = new Properties();
			p.setProperty("hostname", _txtHostname.getText());
			p.setProperty("username", _txtUsername.getText());
			p.store(fout, "Do not edit this file!");
			fout.flush();
			fout.close();
		}
    	catch (FileNotFoundException e)
    	{
			e.printStackTrace();
		}
    	catch (IOException e)
    	{
			e.printStackTrace();
		}
    }
    private void loadConnectionData()
    {
    	try
    	{
			FileInputStream fin = new FileInputStream(saveFile);
			Properties p = new Properties();
			p.load(fin);
			_txtHostname.setText(p.getProperty("hostname"));
			_txtUsername.setText(p.getProperty("username"));
			fin.close();
		}
    	catch (FileNotFoundException e)
    	{
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    }
}
